package edu.cs.usask.ca.silver.plugin.rcp.editors;

import org.apache.log4j.Logger;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;

import edu.cs.usask.ca.silver.businesslogic.ids.ParameterID;
import edu.cs.usask.ca.silver.businesslogic.ids.ScenarioID;
import edu.cs.usask.ca.silver.plugin.rcp.dao.SilverBusinessLogicController;
import edu.cs.usask.ca.silver.plugin.rcp.jobs.LoggerReportScheduler;

/**
 * This class provides the structure for the table viewer displayed in the model
 * version editor pane. The class lays out the column names, order and data
 * associated with parameter within the scenario Again all data that is
 * displayed here is retrieved from the database.
 * 
 * @author Mike Taylor
 * @author yudi.xue@usask.ca
 * 
 */
public class TableColumnLabelProvider {

	public InfoEditor infoEditor;
	public ScenarioID scenarioId;
	private static Logger logger = Logger.getLogger(TableColumnLabelProvider.class);

	public TableColumnLabelProvider(InfoEditor infoEditor, ScenarioID scenarioId) {
		this.infoEditor = infoEditor;
		this.scenarioId = scenarioId;
	}

	/**
	 * A method that actually fleshes out the data to be displayed in the table
	 * viewer for the parameter that belong to a scenario.
	 * 
	 * @param viewer
	 *            the table viewer that these column belong
	 */
	public void createColumns(TableViewer viewer) {

		// Column 0: Name
		TableViewerColumn nameColumn = new TableViewerColumn(viewer, SWT.NONE);
		nameColumn.getColumn().setWidth(150);
		nameColumn.getColumn().setText("Name");
		nameColumn.getColumn().setMoveable(true);
		nameColumn.setLabelProvider(new ColumnLabelProvider() {

			@Override
			public String getText(Object element) {
				try {
					return ((ParameterID) element).getEntryNameFromDatabase(infoEditor.getAssociatedStorage());
				} catch (Exception e) {
					LoggerReportScheduler.scheduleErrorLogging(logger,"error while getting parameter name from db creating column", e);
				}
				return null;
			}
		});

		// Column 1: Type
		TableViewerColumn typeColumn = new TableViewerColumn(viewer, SWT.NONE);
		typeColumn.getColumn().setWidth(75);
		typeColumn.getColumn().setText("Type");
		typeColumn.getColumn().setMoveable(true);
		typeColumn.setLabelProvider(new ColumnLabelProvider() {

			@Override
			public String getText(Object element) {
				String result = null;
				try {
					result = SilverBusinessLogicController
							.borrowParameterObject().getType(
									(ParameterID) element, infoEditor.getAssociatedStorage());
				} catch (Exception e) {
					LoggerReportScheduler.scheduleErrorLogging(logger,"error while getting parameter type from db creating column", e);
				}
				SilverBusinessLogicController.returnLastParameterObject();
				return result;
			}

		});

		// Column 2: Value
		TableViewerColumn valueColumn = new TableViewerColumn(viewer, SWT.NONE);
		valueColumn.getColumn().setWidth(100);
		valueColumn.getColumn().setText("Value");
		valueColumn.getColumn().setMoveable(true);
		valueColumn.setLabelProvider(new ColumnLabelProvider() {

			@Override
			public String getText(Object element) {
				String result = null;
				try {
					result = SilverBusinessLogicController.borrowScenario()
							.getParameterValue(scenarioId,
									(ParameterID) element, infoEditor.getAssociatedStorage());
				} catch (Exception e) {
					LoggerReportScheduler.scheduleErrorLogging(logger,"error while getting parameter value from db creating column", e);
				}
				SilverBusinessLogicController.returnLastScenarioObject();
				return result;
			}
		});

		// Column 3: Units
		TableViewerColumn classColumn = new TableViewerColumn(viewer, SWT.NONE);
		classColumn.getColumn().setWidth(75);
		classColumn.getColumn().setText("Class");
		classColumn.getColumn().setMoveable(true);

		// creatorLabel.setBackground(top.getDisplay().getSystemColor(SWT.COLOR_GRAY));

		classColumn.setLabelProvider(new ColumnLabelProvider() {

			@Override
			public String getText(Object element) {
				String result = null;
				try {
					result = SilverBusinessLogicController
							.borrowParameterObject().getAssociatedClass(
									(ParameterID) element, infoEditor.getAssociatedStorage());
				} catch (Exception e) {
					LoggerReportScheduler.scheduleErrorLogging(logger,"error while getting parameter associated class from db creating column", e);
				}
				SilverBusinessLogicController.returnLastParameterObject();
				return result;
			}

		});

		// Column 4: Units
		TableViewerColumn unitsColumn = new TableViewerColumn(viewer, SWT.NONE);
		unitsColumn.getColumn().setWidth(75);
		unitsColumn.getColumn().setText("Units");
		unitsColumn.getColumn().setMoveable(true);

		// creatorLabel.setBackground(top.getDisplay().getSystemColor(SWT.COLOR_GRAY));

		unitsColumn.setLabelProvider(new ColumnLabelProvider() {

			@Override
			public String getText(Object element) {
				String result = null;
				try {
					result = SilverBusinessLogicController
							.borrowParameterObject().getUnits(
									(ParameterID) element, infoEditor.getAssociatedStorage());
				} catch (Exception e) {
					LoggerReportScheduler.scheduleErrorLogging(logger,"error while getting parameter units from db creating column", e);
				}
				SilverBusinessLogicController.returnLastParameterObject();
				return result;

			}

		});
		/*
		 * This determines if the table viewer is displayed in a scenario pane
		 * of a model version pane. If the pane is for a scenario than the units
		 * column is editable and editor support called. If pane is for a model
		 * version then this is populating the baseline scenario and is not
		 * editable
		 */
		if (this.infoEditor.getObjectId() instanceof ScenarioID) {
			unitsColumn.setEditingSupport(new InfoEditorEditingSupport(viewer,
					"Units", infoEditor));
		}

		// Column 5: Description
		TableViewerColumn descColumn = new TableViewerColumn(viewer, SWT.NONE);
		descColumn.getColumn().setWidth(275);
		descColumn.getColumn().setText("Parameter Comment");
		descColumn.getColumn().setMoveable(true);
		descColumn.setLabelProvider(new ColumnLabelProvider() {

			@Override
			@SuppressWarnings("unchecked")
			public String getText(Object element) {
				String result = null;
				try {
					result = SilverBusinessLogicController.borrowScenario()
							.getParameterComment(scenarioId,
									(ParameterID) element, infoEditor.getAssociatedStorage());
				} catch (Exception e) {
					LoggerReportScheduler.scheduleErrorLogging(logger,"error while getting parameter comment from db creating column", e);
				}
				SilverBusinessLogicController.returnLastScenarioObject();
				return result;

			}

		});
		/* TODO may want to have the description changeable for a sceanrio */
		// descColumn.setEditingSupport(new
		// MOEditorParameterEditingSupport(viewer, DESCRIPTION, _editor));

	}

}/* end of class */
